package com.zingrow.web.report.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.PageView;
import com.zingrow.web.report.base.Cycle;
import com.zingrow.web.report.mapper.RptInfoMapper;
import com.zingrow.web.report.mapper.RptModelMapper;
import com.zingrow.web.report.model.RptInfo;
import com.zingrow.web.report.model.RptModel;
import com.zingrow.web.report.service.IRptInfoService;
import com.zingrow.web.report.service.IRptModelService;
import com.zingrow.web.report.utils.DateUtils;

@Service
public class RptInfoServiceImpl implements IRptInfoService {
    @Autowired
    RptInfoMapper rptInfoMapper;
    @Autowired
    IRptModelService rptModelService;
    @Autowired
    RptModelMapper rptModelMapper;

    @Override
    public RptInfo selectByRptInfoId(Integer id) {
        return rptInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<RptInfo> selectAll(PageView pageView, String rptName, Integer groupId, Integer cycleId, Integer userId,
            Date startTime, Date endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("paging", pageView);
        map.put("groupId", groupId);
        map.put("cycleId", cycleId);
        map.put("rptName", rptName);
        map.put("userId", userId);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<Integer> ids = rptInfoMapper.selectIds(map);
        if (ids.size() < 1) {
            ids.add(-1);
        }
        List<RptInfo> list = rptInfoMapper.selectAllByIds(ids,userId);
        return list;
    }

    @Override
    public List<RptInfo> createDayRpt(Date createTime, Date startTime) {
        // 拿到对应报告开始的时间
        if (startTime == null) {
            Calendar calendar = DateUtils.getZero(createTime);
            calendar.add(Calendar.DATE, -1);
            startTime = calendar.getTime();
        }
        List<RptModel> models = rptModelService.selectAll(null, null, null, null, Cycle.DAY.getCode(), 1);
        return createRptInfo(models, startTime, createTime);
    }

    @Override
    public List<RptInfo> createWeekRpt(Date createTime, Date startTime) {
        // 拿到对应报告开始的时间
        if (startTime == null) {
            Calendar calendar = DateUtils.getZero(createTime);
            calendar.add(Calendar.DATE, -7);
            DateUtils.setToFirstDay(calendar);
            startTime = calendar.getTime();
        }
        List<RptModel> models = rptModelService.selectAll(null, null, null, null, Cycle.WEEK.getCode(), 1);
        return createRptInfo(models, startTime, createTime);
    }

    @Override
    public List<RptInfo> createMonthRpt(Date createTime, Date startTime) {
        // 拿到对应报告开始的时间
        if (startTime == null) {
            Calendar calendar = DateUtils.getZero(createTime);
            calendar.add(Calendar.MONTH, -1);
            startTime = DateUtils.getMonthStart(calendar.getTime());
        }
        List<RptModel> models = rptModelService.selectAll(null, null, null, null, Cycle.MONTH.getCode(), 1);
        return createRptInfo(models, startTime, createTime);
    }

    @Override
    public List<RptInfo> createQuartRpt() {
        return null;
    }

    @Override
    public List<RptInfo> createHarfYearRpt() {
        return null;
    }

    @Override
    public List<RptInfo> createYearRpt() {
        return null;
    }

    /**
     * 把模版转换为报告列表并插入数据库
     */
    private List<RptInfo> createRptInfo(List<RptModel> rptModels, Date startTime, Date createTime) {
        List<RptInfo> rptInfoList = new ArrayList<>();
        for (RptModel rptModel : rptModels) {
            RptInfo rptInfo = new RptInfo(rptModel.getName() + DateUtils.printDate(startTime), rptModel.getId(),
                    startTime, createTime, "");
            // 查询重复的 ,若有重复的则不添加新的报告信息
            Map<String, Object> map = new HashMap<>();
            map.put("modelId", rptInfo.getModelId());
            map.put("startTime", rptInfo.getStartTime());
            List<RptInfo> list = rptInfoMapper.selectSameRptInfo(map);
            if (list.isEmpty()) {
                rptInfoMapper.insert(rptInfo);
                rptInfo.setModuleSort(rptModel.getModuleSort());
                rptInfoList.add(rptInfo);
            }
        }
        return rptInfoList;
    }

    @Override
    public boolean delete(Integer id) {
        return rptInfoMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public List<RptInfo> selectUserRptById(Integer id) {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(id);
        List<RptInfo> list = rptInfoMapper.selectAllByIds(ids,null);
        return list;
    }

	@Override
	public List<RptInfo> selectAllByIduserId(PageView pageView, String rptName, Integer groupId, Integer cycleId, Integer userId,
            Date startTime, Date endTime) {
		Map<String,Object> map = new HashMap<String, Object>();
        map.put("paging", pageView);
        map.put("groupId", groupId);
        map.put("cycleId", cycleId);
        map.put("rptName", rptName);
        map.put("userId", userId);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<Integer> ids = rptInfoMapper.selectIds(map);
        if (ids.size() < 1) {
            ids.add(-1);
        }
        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("userId", userId);
        map2.put("ids", ids);
        map2.put("cycleId", cycleId);
        List<RptInfo> list = rptInfoMapper.selectAllByIduserId(map2);
		return list;
	}

}
